package com.ilop.sthome.mvp.present.device;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.text.TextUtils;

import com.example.common.mvp.BasePresenterImpl;
import com.ilop.sthome.common.CommonId;
import com.ilop.sthome.common.command.SendDeviceCommand;
import com.ilop.sthome.data.enums.SmartDevice;
import com.ilop.sthome.data.greenDao.DeviceInfoBean;
import com.ilop.sthome.data.greenDao.SceneBean;
import com.ilop.sthome.mvp.contract.device.GatewayContract;
import com.ilop.sthome.ui.activity.detail.AlarmDeviceActivity;
import com.ilop.sthome.ui.activity.detail.lock.LockDetailActivity;
import com.ilop.sthome.ui.activity.detail.OutDoorDetailActivity;
import com.ilop.sthome.ui.activity.detail.socket.SocketDetailActivity;
import com.ilop.sthome.ui.activity.detail.THCheckDetailActivity;
import com.ilop.sthome.ui.activity.detail.thermostat.TempControlDetailActivity;
import com.ilop.sthome.utils.CoderUtils;
import com.ilop.sthome.utils.greenDao.utils.DeviceDaoUtil;
import com.ilop.sthome.utils.greenDao.utils.SceneDaoUtil;
import com.siterwell.familywellplus.R;

import java.util.List;

/**
 * @author skygge
 * @date 2020-01-02.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：
 */
public class GatewayPresenter extends BasePresenterImpl<GatewayContract.IView> implements GatewayContract.IPresent {

    private Context mContext;
    private String mDeviceName;
    private DeviceInfoBean mGateway;
    private Handler mHandler;
    private SendDeviceCommand mSend;

    public GatewayPresenter(Context mContext, String deviceName) {
        this.mContext = mContext;
        this.mDeviceName = deviceName;
        mHandler = new Handler();
        mGateway = DeviceDaoUtil.getInstance().findGatewayByDeviceName(mDeviceName);
        mSend = new SendDeviceCommand(mContext, mGateway);
    }

    @Override
    public void getDeviceState() {
        int state = mGateway.getStatus();
        String name = TextUtils.isEmpty(mGateway.getNickName())? mContext.getString(R.string.ali_gateway):mGateway.getNickName();
        SceneBean mScene = SceneDaoUtil.getInstance().findSceneByChoice(mDeviceName);
        String mode ;
        boolean isShared = mGateway.getOwned() != 1;
        String current = mContext.getString(R.string.current_scene) + ":";
        if(mScene==null){
            mode = current + mContext.getString(R.string.home_mode);
        }else {
            switch (mScene.getSid()){
                case 0:
                    mode = current + mContext.getString(R.string.home_mode);
                    break;
                case 1:
                    mode = current + mContext.getString(R.string.out_mode);
                    break;
                case 2:
                    mode = current + mContext.getString(R.string.sleep_mode);
                    break;
                default:
                    mode = current + mScene.getModleName();
                    break;
            }
        }
        mView.refreshState(name, state, mode, isShared);
    }

    @Override
    public void gatewayTextAlarm() {
        mSend.sendEquipmentCommand(0, "33000000");
    }

    @Override
    public void gatewayMute() {
        mSend.sendGateWaySilence();
    }

    @Override
    public void getGatewayResponse() {
        if (mGateway.getStatus() == 1){
            mHandler.post(()-> mView.showToastMsg(mContext.getString(R.string.success_test)));
        }else {
            mHandler.post(()-> mView.showToastMsg(mContext.getString(R.string.device_has_no_response)));
        }
    }

    @Override
    public void startSynchronization() {
        String crc = CoderUtils.getDeviceCRC(mDeviceName);
        String timezone = CoderUtils.getTimeZone();
        mSend.synAllDeviceStatus(crc, timezone);
    }

    @Override
    public void synchronizationNickName(String deviceName) {
        String crc = CoderUtils.getDeviceNameCRC(deviceName);
        mSend.synGetDeviceName(crc);
    }

    @Override
    public void synchronizationGatewayRoom(String deviceName) {
        mSend.queryOrSetGatewayInfo("00", "00");
    }

    @Override
    public void findAllSubDevice() {
        List<DeviceInfoBean> deviceList = DeviceDaoUtil.getInstance().findAllSubDevice(mDeviceName);
        if (deviceList.size()>0){
            mHandler.post(()-> mView.refreshSubList(deviceList, mGateway.getOwned() != 1));
        }else {
            mHandler.post(()-> mView.refreshNoList(mGateway.getOwned() != 1));
        }
    }

    @Override
    public void getGatewayRoom(String roomId) {
        if (Integer.parseInt(roomId, 16) > 9){
            mGateway.setRoom_id("00");
        }else {
            mGateway.setRoom_id(roomId);
        }
        DeviceDaoUtil.getInstance().getDeviceDao().update(mGateway);
    }

    @Override
    public void clickItem(DeviceInfoBean device) {
        SmartDevice mDevice = SmartDevice.getType(device.getDevice_type());
        Intent intent = new Intent();
        intent.putExtra(CommonId.KEY_DEVICE_NAME, device.getDeviceName());
        intent.putExtra(CommonId.KEY_DEVICE_ID, device.getDevice_ID());
        switch (mDevice){
            case EE_DEV_PIR:
            case EE_DEV_SOS:
            case EE_DEV_DOOR:
            case EE_DEV_BUTTON:
            case EE_DEV_CO_ALARM:
            case EE_DEV_SM_ALARM:
            case EE_DEV_WT_ALARM:
            case EE_DEV_GAS_ALARM:
            case EE_DEV_NEW_GAS_ALARM:
            case EE_DEV_CO_GAS_ALARM:
            case EE_DEV_MODE_BUTTON:
            case EE_DEV_SX_SM_ALARM:
            case EE_DEV_THERM_ALARM:
            case EE_DEV_FLASH_ALARM:
                intent.setClass(mContext, AlarmDeviceActivity.class);
                mView.skipActivity(intent);
                break;
            case EE_DEV_LOCK:
                intent.setClass(mContext, LockDetailActivity.class);
                mView.skipActivity(intent);
                break;
            case EE_DEV_SOCKET:
            case EE_DEV_MANIPULATOR:
            case EE_DEV_SOLENOID_VALVE:
                intent.setClass(mContext, SocketDetailActivity.class);
                mView.skipActivity(intent);
                break;
            case EE_DEV_TH_CHECK:
                intent.setClass(mContext, THCheckDetailActivity.class);
                mView.skipActivity(intent);
                break;
            case EE_TEMP_CONTROL:
                intent.setClass(mContext, TempControlDetailActivity.class);
                mView.skipActivity(intent);
                break;
            case EE_TEMP_OUTDOOR_SIREN:
                intent.setClass(mContext, OutDoorDetailActivity.class);
                mView.skipActivity(intent);
                break;
        }
    }
}
